package logic

import (
	"context"
	"errors"
	"video-management-system/model"

	"video-management-system/userrpc/internal/svc"
	"video-management-system/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
	return &UpdateUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 个人资料修改
func (l *UpdateUserLogic) UpdateUser(in *userrpc.UpdateUserRequest) (*userrpc.UpdateUserResponse, error) {
	// todo: add your logic here and delete this line
	var findUser model.User
	err := findUser.FindUserById(model.DB, in.UserId)
	if err != nil {
		return nil, err
	}
	if findUser.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	findUser.Nickname = in.Nickname

	if err = findUser.UpdateUser(model.DB, in.UserId); err != nil {
		return nil, errors.New("修改失败")
	}
	return &userrpc.UpdateUserResponse{
		UserId: int64(findUser.ID),
	}, nil
}
